//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>

#include "libraries.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE 100000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main(int argc, char** argv) {

  String* list;
  String* namefileIn;
  String* namefileOut;

  if (argc < 4) {
    cout << "Error in the input arguments. The execution must be: " << endl;
    cout << "./ReduceSmileFileByList <list> <input file> <output file>" << endl;
    return 0;
  }
  else {
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << "  List of ID : " << argv[1] << endl;
    cout << "  Input File : " << argv[2] << endl;
    cout << " Output File : " << argv[3] << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    list        = new String(argv[1]);
    namefileIn  = new String(argv[2]);
    namefileOut = new String(argv[3]);
  }

  fstream          fileList;
  char             buffer[LENGTHLINE + 1];
  TListE <String>* listFiles = new TListE <String> ();

  fileList.open(list->Out(), fstream::in);
  while (!fileList.eof()) {
    fileList.getline(buffer, LENGTHLINE);
    String* line = new String(buffer);
    line->RTrim();
    if (!line->Length()) {
      if (line) delete line;
      continue;
    }
    listFiles->Add(line);
  }
  fileList.close();

  fstream fileSmile, fileOut;

  fileOut.open(namefileOut->Out(), fstream::out);
  if (listFiles->Length()) {
    listFiles->SetInitial();
    while (listFiles->SetNext()) {
      listFiles->GetCurrent()->RTrim();
      cout << "Search: " << listFiles->GetCurrent()->Out() << endl;
      fileSmile.open(namefileIn->Out(), fstream::in);
      while (!fileSmile.eof()) {
        fileSmile.getline(buffer, LENGTHLINE);
        if (strlen(buffer) < 4) continue;
        if (strstr(buffer, listFiles->GetCurrent()->Out())) {
          char* part = strtok(buffer, " >");
          fileOut << part << " " << listFiles->GetCurrent()->Out() << endl;
          cout << part << " " << listFiles->GetCurrent()->Out() << endl;
          break;
        }
      }
      fileSmile.close();
    }
  }

  fileOut.close();

  cout << "---------------------------------------------------------------------------------" << endl;

  if (list)        delete list;
  if (namefileIn)  delete namefileIn;
  if (namefileOut) delete namefileOut;

  return 1;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//



